package com.fight.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * @ClassName: SortListUtil
 * @Description: 列表的通用过滤函数
 * @author: 温泉
 * @company: 上海航盛实业有限公司
 * @date 2012-04-06 09:19:45 +0800
 * @param <E>
 */
public class SortListUtil<E> {

    /**
     * @Fields log : log4j句柄
     */
    protected Logger log = Logger.getLogger(getClass());

    /**
     * @Title: sort
     * @Description: 排序方法
     * @param List
     *            <E> list
     * @param final String method
     * @param final String sort 设定文件
     * @return void 返回类型
     * @author: 温泉
     * @date 2012-04-06 09:20:02 +0800
     * @throws
     */
    @SuppressWarnings("unchecked")
    public void sort(List<E> list, final String method, final String sort) {

        // 调用比较函数
        Collections.sort(list, new Comparator() {
            public int compare(Object a, Object b) {
                // 标志位
                int ret = 0;

                try {

                    Method m1 = ((E) a).getClass().getMethod(method, null);

                    Method m2 = ((E) b).getClass().getMethod(method, null);

                    if (sort != null && "desc".equals(sort)) {// 倒序
                        ret = m2.invoke(((E) b), null).toString().compareTo(m1.invoke(((E) a), null).toString());
                    } else {// 正序
                    // ret = m1.invoke(((E)a),
                    // null).toString().compareTo(m2.invoke(((E)b),
                    // null).toString());
                        int tmp1 = Integer.parseInt(m1.invoke(((E) a), null).toString());
                        int tmp2 = Integer.parseInt(m2.invoke(((E) b), null).toString());
                        if (tmp1 > tmp2) {
                            ret = 1;
                        } else if (tmp1 < tmp2) {
                            ret = -1;
                        } else {
                            ret = 0;
                        }
                    }

                } catch (NoSuchMethodException ne) {
                    log.error(ne.getMessage());
                } catch (IllegalAccessException ie) {
                    log.error(ie.getMessage());
                } catch (InvocationTargetException it) {
                    log.error(it.getMessage());
                }
                return ret;
            }
        });
    }
    /**
     * @Title: sortDouble
     * @Description: double类型排序方法
     * @param List
     *            <E> list
     * @param final String method
     * @param final String sort 设定文件
     * @return void 返回类型
     * @author: 黎霞
     * @date 2014-03-17 09:20:02 +0800
     * @throws
     */
    @SuppressWarnings("unchecked")
    public void sortDouble(List<E> list, final String method, final String sort) {

        // 调用比较函数
        Collections.sort(list, new Comparator() {
            public int compare(Object a, Object b) {
                // 标志位
                int ret = 0;

                try {

                    Method m1 = ((E) a).getClass().getMethod(method, null);

                    Method m2 = ((E) b).getClass().getMethod(method, null);

                    if (sort != null && "desc".equals(sort)) {// 倒序
                        Double tmp1 = Double.valueOf(m1.invoke(((E) a), null).toString());
                        Double tmp2 = Double.valueOf(m2.invoke(((E) b), null).toString());
                       ret=-tmp1.compareTo(tmp2);
                    } else {// 正序
                        Double tmp1 = Double.valueOf(m1.invoke(((E) a), null).toString());
                        Double tmp2 = Double.valueOf(m2.invoke(((E) b), null).toString());
                        if (tmp1 > tmp2) {
                            ret = 1;
                        } else if (tmp1 < tmp2) {
                            ret = -1;
                        } else {
                            ret = 0;
                        }
                    }

                } catch (NoSuchMethodException ne) {
                    log.error(ne.getMessage());
                } catch (IllegalAccessException ie) {
                    log.error(ie.getMessage());
                } catch (InvocationTargetException it) {
                    log.error(it.getMessage());
                }
                return ret;
            }
        });
    }
    @SuppressWarnings("unchecked")
	public void sortDate(List<E> list, final String method, final String sort) {

        // 调用比较函数
        Collections.sort(list, new Comparator() {
            public int compare(Object a, Object b) {
                // 标志位
                int ret = 0;

                try {

                    Method m1 = ((E) a).getClass().getMethod(method, null);

                    Method m2 = ((E) b).getClass().getMethod(method, null);

                    if (sort != null && "desc".equals(sort)) {// 倒序
                        Date tmp1 = new Date(m1.invoke(((E) a), null).toString());
                        Date tmp2 = new Date(m2.invoke(((E) b), null).toString());
                       ret=-tmp1.compareTo(tmp2);
                    } else {// 正序
                    	 Date tmp1 = new Date(m1.invoke(((E) a), null).toString());
                         Date tmp2 = new Date(m2.invoke(((E) b), null).toString());
                        if (tmp1.after(tmp2)) {
                            ret = 1;
                        } else if (tmp1.before(tmp2)) {
                            ret = -1;
                        } else {
                            ret = 0;
                        }
                    }

                } catch (NoSuchMethodException ne) {
                    log.error(ne.getMessage());
                } catch (IllegalAccessException ie) {
                    log.error(ie.getMessage());
                } catch (InvocationTargetException it) {
                    log.error(it.getMessage());
                }
                return ret;
            }
        });
    }
}
